package com.example.testproject.activity;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;

import com.example.testproject.R;


/**
 * @ClassName: AsyncTaskActivity
 * @Author: android 超级兵
 * @CreateDate: 1/14/22$ 2:39 PM$
 * TODO AsyncTask源码分析
 */
public class AsyncTaskActivity extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_async_task);

        AppCompatImageView imageView = findViewById(R.id.asyncImage);
        TextView asyncTv = findViewById(R.id.asyncTv);
        SeekBar seekBar = findViewById(R.id.seekBar);
        Button asyncBt = findViewById(R.id.asyncBt);

        asyncBt.setOnClickListener(v -> {
            MyAsyncTask myAsyncTask = new MyAsyncTask(imageView, asyncTv, seekBar);
            myAsyncTask.execute();
        });

//
//        SparseArray<String> sparseArray = new SparseArray<>();
//        sparseArray.put(0, "");
//        HashMap<String, String> hashMap = new HashMap<>();
//        hashMap.put("", "");
//        hashMap.get("");
//
//        // 最低api23 使用
//        ArrayMap<String, String> stringStringArrayMap = new ArrayMap<>();
//        stringStringArrayMap.append("", "");
//
//        Integer integer = new Integer(1);
//        Integer.valueOf("1");
//        Double.valueOf(1.2);
//        Float.valueOf(1.2f);
//
//        Long.valueOf(1000_1000);
    }

    @SuppressLint("StaticFieldLeak")
    public static class MyAsyncTask extends AsyncTask<String, Integer, String> {
        private final ImageView imageView;
        private final TextView textView;
        private final SeekBar seekBar;

        public MyAsyncTask(AppCompatImageView imageView, TextView textView, SeekBar seekBar) {
            this.imageView = imageView;
            this.textView = textView;
            this.seekBar = seekBar;
        }

        @Override
        protected void onPreExecute() {
//            super.onPreExecute();
            // Main线程
            // 开始前的操作。。。
            textView.setText("要准备开始啦。。。");
            imageView.setImageResource(R.mipmap.no_data);
        }

        @Override
        protected void onPostExecute(String s) {
//            super.onPostExecute(s);
            textView.setText("结束啦。。。");
        }

        @Override
        protected String doInBackground(String... strings) {
            try {
                for (int i = 0; i < 10; i++) {
                    publishProgress(i);
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
//            super.onProgressUpdate(values);
            textView.setText(String.valueOf(values[0]));
            seekBar.setProgress(values[0]);
        }
    }
}
